<template>
  <div>
    <el-container>
      <el-aside :style="{ width: isCollapse ? 'auto' : '200px' }">
        <navbar :menus="menus" :collapse="isCollapse"></navbar>
      </el-aside>
      <el-container>
        <el-header>
          <i
            class="el-collapse el-icon-s-fold"
            :class="isCollapse ? 'collapseTransform' : ''"
            @click="handleCollapse"
          ></i>

          <!-- 下拉菜单 -->
          <el-dropdown>
            <span class="el-dropdown-link">
              用户操作<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>用户信息</el-dropdown-item>
              <el-dropdown-item>修改密码</el-dropdown-item>
              <el-dropdown-item @click.native="handleLogout">注销</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-header>
        <el-main>
          <router-view></router-view>

        </el-main>

        <!-- 底部条 -->
        <el-footer>
          Copyright © 2021 - 2021 🐟. All Rights Reserved. 爱🐟科技 版权所有
        </el-footer>
      </el-container>
    </el-container>
    <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="30%"
          >
            <span>是否要注销当前用户？要记得保存哦！</span>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="handleConfirmLogout"
                >确 定</el-button
              >
            </span>
          </el-dialog>
  </div>
</template>
<script>
import routes from "../router/routes";
import Navbar from "../components/Navbar";
export default {
  components: {
    Navbar,
  },
  data() {
    return {
      isCollapse: false,
      dialogVisible: false,
    };
  },
  methods: {
    resolve(arr, parentPath = "") {
      let newArr = [];
      //第一步遍历数组当中的每一个元素
      arr.forEach((route) => {
        let obj = Object.assign({}, route);

        // 当上级路径存在，并且最后一位不为/，则后面添加 /
        parentPath =
          parentPath && parentPath.lastIndexOf("/") !== parentPath.length - 1
            ? parentPath + "/"
            : parentPath;

        // 总是给加上父级路径
        obj.path = parentPath + obj.path;
        // 第二步 判断有没有children属性 如有，那么需要再处理，如果没有则直接push到一个新数组中
        if (obj.children && obj.children.length > 0) {
          obj.children = this.resolve(obj.children, obj.path);
        }
        //如果当前路由 有hidden ，那就它的子节点提到最上面来
        if (obj.meta && obj.meta.hidden) {
          if (obj.children && obj.children.length > 0) {
            obj.children.forEach((item) => {
              let tmp = Object.assign({}, item);
              newArr.push(tmp);
            });
          }
        } else {
          newArr.push(obj);
        }
        
      });
      return newArr;
    },
    handleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    handleLogout() {
      this.dialogVisible = true;
    },
    handleConfirmLogout(){
      this.dialogVisible = false;
      localStorage.removeItem('accessToken')  //应该是账号 清除token值
      localStorage.removeItem('refreshToken') //应该是密码 清除token值
      this.$router.push('/login')   //返回登陆页面
    },
  },
  computed: {
    menus() {
      let arr = this.resolve(routes);
      return arr;
    },
  },
};
</script>

<style scoped>
.el-aside {
  height: 100%;
}

.el-header {
  background-color: rgba(83, 203, 243, 0.774);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.el-main {
  /* background-color: brown; */
  min-height: calc(100vh - 120px);
}

.el-footer {
  background-color: rgba(83, 203, 243, 0.774);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 宋体;
  font-size: 15px;
}
.el-collapse {
  font-size: 2rem;
}
.collapseTransform {
  transform: rotate(-180deg);
}

/* 下拉菜单的属性 */
.el-dropdown-link {
  cursor: pointer;
  color: #fafafa;
}
.el-icon-arrow-down {
  font-size: 15px;
}
</style>
